package com.hzit.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 类名：
 * 作者：WF
 * 功能：登录拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		//1. 从session中得到当前登录的用户名，如果存在，就证明登录成功过,就放行，否则，就重新跳转到登录页面
		HttpSession session = request.getSession();
		Object username = session.getAttribute("username");
		if(username == null){   // 代表未登录,跳转到登录页面
			session.setAttribute("msg","您未登录，请登录！");
			response.sendRedirect("/login.jsp");
			return false;
		}
		//2. 如果存在用户名就证明登录成功过，直接放行
		return true;
	}
}
